package com.sskj.tibi.presenter;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;

import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.tibi.bean.AssetListBean;
import com.sskj.tibi.bean.AssetTopBean;
import com.sskj.tibi.http.HttpConfig;
import com.sskj.tibi.ui.activity.AssetManageActivity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;


public class AssetManageActivityPresenter extends BasePresenter<AssetManageActivity> {
    public Flowable<List<AssetListBean>> getData(String p, String size) {
        return OkGo.<HttpData<List<AssetListBean>>>get(HttpConfig.BASE_URL + HttpConfig.ASEET_LIST)
                .params("id", SPUtil.get(SPConfig.ID, ""))
//                .params("p", p)
//                .params("size", size)
                .converter(new JsonConverter<HttpData<List<AssetListBean>>>() {
                })
                .tag(this)
                .adapt(new FlowableBody<>())
                .map(aseetListBeanHttpData -> aseetListBeanHttpData.getData())
                .flatMap(assetListBeans -> Flowable.fromIterable(assetListBeans))
//                .map(assetListBean -> {
//                    if (assetListBean.getStockCode().equals("USDT")) {
//                        assetListBean.setPriority(2);
//                    } else if (assetListBean.getStockCode().equals("FEC")) {
//                        assetListBean.setPriority(1);
//                    } else {
//                        assetListBean.setPriority(0);
//                    }
//                    return assetListBean;
//                })
                .sorted((o1, o2) -> o2.getPriority() - o1.getPriority())
                .toList()
                .toFlowable()
                .onErrorReturnItem(new ArrayList());
    }

    public void getTop() {
        OkGo.<HttpData<AssetTopBean>>get(HttpConfig.BASE_URL + HttpConfig.ASEET_TOP)
                .execute(new JsonCallBack<HttpData<AssetTopBean>>(this, false) {
                    @Override
                    public void onSuccess(Response<HttpData<AssetTopBean>> response) {
                        HttpData<AssetTopBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateUI(httpData.getData());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<AssetTopBean>> response) {
                        super.onError(response);
                    }
                });

    }


}
